layui.use(['form','treeSelect','layer','upload','laydate'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload;
    var treeSelect = layui.treeSelect;
    var laydate = layui.laydate;

    //回显数据
    var request = GetRequest();
    $.get("/equipment/" + request.id,function (data) {

        $("#id").val(data.id);
        $("#name").val(data.name);
        $("#number").val(data.number);
        $("#serial").val(data.serial);
        $("#model").val(data.model);
        $("#imsiCard").val(data.imsiCard);
        $("#remarks").val(data.remarks);

        //处理日期时间信息
        var date = new Date(data.purchaseTime);//购买日期
        laydate.render({
            elem: '#purchaseTime'
            ,value: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() //必须遵循format参数设定的格式
        });
        date = new Date(data.effectTime);//生效日期
        laydate.render({
            elem: '#effectTime'
            ,value: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() //必须遵循format参数设定的格式
        });
        date = new Date(data.effectTime);//到期日期
        laydate.render({
            elem: '#expirationTime'
            ,value: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() //必须遵循format参数设定的格式
        });

        //所属场景
        treeSelect.render({
            elem: '#tree-select-scenery',
            data: '/scene/tree/select',
            type: 'get',
            placeholder: '请选择场景',// 占位符
            search: true,// 是否开启搜索功能：true/false，默认false
            click: function(data){
                $("#tree-select-scenery").val(data.current.id);
            },
            success: function (item) { // 加载完成后的回调函数 areaId
                if (data.sceneId != null && data.sceneId != "") {
                    $("#tree-select-scenery").val(data.sceneId);
                } else {
                    $("#tree-select-scenery").val("0");
                }
                treeSelect.checkNode('tree-select-scenery',data.sceneId);
            }
        });

        //场景类型
        treeSelect.render({
            elem: '#tree-select-type',
            data: '/equipment/type/tree/select',
            type: 'get',
            placeholder: '顶级分类',// 占位符
            search: true,// 是否开启搜索功能：true/false，默认false
            click: function(data){
                $("#tree-select-type").val(data.current.id);
            },
            success: function (item) { // 加载完成后的回调函数
                if (data.typeId != null && data.typeId != "") {
                    $("#tree-select-type").val(data.typeId);
                } else {
                    $("#tree-select-type").val("0");
                }
                treeSelect.checkNode('tree-select-type',data.typeId);
            }
        });

        //加载电池类型信息
        $.get("/dictionary/list/e5dadb56928d496c8e7c78a5f16a93aa",function (status) {
            $.each(status.data,function (i,ele) {
                if (ele.value == data.powerType) {
                    $("#powerType").append('<option selected="selected" value="'+ ele.value +'">'+ ele.name +'</option>');
                } else {
                    $("#powerType").append('<option value="'+ ele.value +'">'+ ele.name +'</option>');
                }
            });
            form.render('select');
        });

        //加载厂家列表信息
        $.get("/manufator/list",function (status) {
            $.each(status.data,function (i,ele) {
                if (ele.value == data.powerType) {
                    $("#manufactorId").append('<option selected="selected" value="'+ ele.value +'">'+ ele.name +'</option>');
                } else {
                    $("#manufactorId").append('<option value="'+ ele.value +'">'+ ele.name +'</option>');
                }
            });
            form.render('select');
        });

    });

    //修改场景信息
    form.on('submit(edit)', function(data){
        console.log(data);
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            url: '/equipment/edit',
            data:data.field,
            type:'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("修改失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("修改失败！");
            }
        });
        return false;
    });

    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


});